package hn.info.hockeynews.helpers;

import java.util.ArrayList;
import java.util.List;

import hn.info.hockeynews.dao.DaoMaster;
import hn.info.hockeynews.dao.DaoMaster.DevOpenHelper;
import hn.info.hockeynews.dao.DaoSession;
import hn.info.hockeynews.dao.Match;
import hn.info.hockeynews.dao.MatchDao;
import hn.info.hockeynews.dao.MatchDao.Properties;
import hn.info.hockeynews.dao.EntryDao;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBHelperMatches {

	private DaoMaster daoMaster;
    private DaoSession daoSession;
    private MatchDao matchDao;
    private EntryDao entryDao;
    private Cursor cursor;
	private SQLiteDatabase db;
	private List<Match> matches; 
	private List<Match> m;
	
	/**
	 * check if there is any data in db
	 * */
	public String checkData() {
		try{
			m = matchDao.queryBuilder().where(Properties.Active.eq(1)).orderAsc(Properties.Match_id).list();
		}
		catch(Exception e) {
			return "null";
		}
		if(m.size() == 0) {
			return "null";
		}
		else {
			return "notnull";
		}
	}

	/**
	 * get all match data
	 * */
	public List<Match> getMatchData() {
		try {
			matches = matchDao.queryBuilder().where(Properties.Active.eq(1)).orderAsc(Properties.Match_id).list();
			return matches;
		}
		catch(Exception e) {
			return null;
		}
	}
	
	/**
	 * get specific match data for your favourite team (not in use yet)
	 * */
	public List<Match> getSpecMatchData(String team) {
		try {
			matches = matchDao.queryBuilder().where(Properties.Home_name.eq(team), Properties.Away_name.eq(team)).list();
			return matches;
		}
		catch(Exception e) {
			return null;
		}
	}
	
	/**
	 * insert a new match to db
	 * */
	public void insertMatch(Match m, DevOpenHelper helper) {
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        matchDao = daoSession.getMatchDao();
        
        matchDao.insertOrReplace(m);

		Log.d("DB", "Added :"+m.getMatch_id()+ "Time: "+m.getTimestamp()+" Data :"+m.toString());
	}
	
	public void closeDB() {
		db.close();
	}

	/**
	 * get names of the teams
	 * */
	public List<String> getTeamNames() {
		List<String> tmplist = new ArrayList<String>();
		try {
			List<Match> qbh = getMatchData();
				
			//matchDao.queryRaw("SELECT "+MatchDao.Properties.Away_name+", "+MatchDao.Properties.Home_name+" FROM "+matchDao.getTablename());
			for(Match m : qbh) {
				tmplist.add("hallo");
				Log.d("Teamnames:" , "added: "+m.getHome_name()+" and "+m.getAway_name());
			}
		}
		catch(Exception e) {
			tmplist.add("No Data");
		}
		return tmplist;
	}
	
	public void killDB(DevOpenHelper helper) {
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        matchDao = daoSession.getMatchDao();
        
        matchDao.deleteAll();

		//Log.d("DB", "Added :"+m.getMatch_id()+ "Time: "+m.getTimestamp()+" Data :"+m.toString());
	}
}
